package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.beans.KeywordStats;
import com.atguigu.gmall.publisher.beans.ProductStats;
import com.atguigu.gmall.publisher.beans.ProvinceStats;
import com.atguigu.gmall.publisher.beans.VisitorStats;
import com.atguigu.gmall.publisher.service.KeywordStatsService;
import com.atguigu.gmall.publisher.service.ProductStatsService;
import com.atguigu.gmall.publisher.service.ProvinceStatsService;
import com.atguigu.gmall.publisher.service.VisitorStatsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * Author: Felix
 * Date: 2021/12/10
 * Desc: 大屏展示控制类
 */
@RestController
@RequestMapping("/api/sugar")
public class SugarController {
    @Autowired
    private ProductStatsService productStatsService;

    @Autowired
    private ProvinceStatsService provinceStatsService;

    @Autowired
    private KeywordStatsService keywordStatsService;

    @Autowired
    private VisitorStatsService visitorStatsService;

    @RequestMapping("/keyword")
    public String getKeyword(
        @RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "20") Integer limit) {

        if (date == 0) {
            date = now();
        }

        List<KeywordStats> keywordStatsList = keywordStatsService.getKeyword(date, limit);
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": [");
        for (int i = 0; i < keywordStatsList.size(); i++) {
            KeywordStats keywordStats = keywordStatsList.get(i);
            jsonB.append("{\"name\": \""+keywordStats.getKeyword()+"\",\"value\": "+keywordStats.getCt()+"}");
            if(i < keywordStatsList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("]}");
        return jsonB.toString();
    }
    @RequestMapping("/hr")
    public String getVisitorStatsByHr(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = now();
        }

        List<VisitorStats> visitorStatsList = visitorStatsService.getVisitorStatsByHr(date);

        VisitorStats[] visitorStatsArr = new VisitorStats[24];

        for (VisitorStats visitorStats : visitorStatsList) {
            visitorStatsArr[visitorStats.getHr()] = visitorStats;
        }

        List hrList = new ArrayList();
        List pvList = new ArrayList();
        List uvList = new ArrayList();
        List svList = new ArrayList();

        //对数组进行遍历   取出某天每一个小时的访问情况
        for (int hr = 0; hr < visitorStatsArr.length; hr++) {
            VisitorStats visitorStats = visitorStatsArr[hr];
            if(visitorStats != null){
                pvList.add(visitorStats.getPv_ct());
                uvList.add(visitorStats.getUv_ct());
                svList.add(visitorStats.getSv_ct());
            }else{
                pvList.add(0L);
                uvList.add(0L);
                svList.add(0L);
            }
            hrList.add(String.format("%02d",hr));
        }

        //拼接字符串
        String json = "{\"status\":0,\"data\":{" + "\"categories\":" +
            "[\""+StringUtils.join(hrList,"\",\"")+ "\"],\"series\":[" +
            "{\"name\":\"uv\",\"data\":["+ StringUtils.join(uvList,",") +"]}," +
            "{\"name\":\"pv\",\"data\":["+ StringUtils.join(pvList,",") +"]}," +
            "{\"name\":\"sv\",\"data\":["+ StringUtils.join(svList,",") +"]}]}}";
        return  json;


    }

    @RequestMapping("/isnew")
    public String getVisitorStatsByIsNew(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = now();
        }

        VisitorStats newVisitorStats = new VisitorStats();
        VisitorStats oldVisitorStats = new VisitorStats();

        List<VisitorStats> visitorStatsList = visitorStatsService.getVisitorStatsByIsNew(date);
        for (VisitorStats visitorStats : visitorStatsList) {
            if(visitorStats.getIs_new().equals("1")){
                newVisitorStats = visitorStats;
            }else{
                oldVisitorStats = visitorStats;
            }
        }
        String json = "{\"status\":0,\"data\":{\"combineNum\":1,\"columns\":" +
            "[{\"name\":\"类别\",\"id\":\"type\"}," +
            "{\"name\":\"新用户\",\"id\":\"new\"}," +
            "{\"name\":\"老用户\",\"id\":\"old\"}]," +
            "\"rows\":" +
            "[{\"type\":\"用户数(人)\"," +
            "\"new\": " + newVisitorStats.getUv_ct() + "," +
            "\"old\":" + oldVisitorStats.getUv_ct() + "}," +
            "{\"type\":\"总访问页面(次)\"," +
            "\"new\":" + newVisitorStats.getPv_ct() + "," +
            "\"old\":" + oldVisitorStats.getPv_ct() + "}," +
            "{\"type\":\"跳出率(%)\"," +
            "\"new\":" + newVisitorStats.getUjRate() + "," +
            "\"old\":" + oldVisitorStats.getUjRate() + "}," +
            "{\"type\":\"平均在线时长(秒)\"," +
            "\"new\":" + newVisitorStats.getDurPerSv() + "," +
            "\"old\":" + oldVisitorStats.getDurPerSv() + "}," +
            "{\"type\":\"平均访问页面数(人次)\"," +
            "\"new\":" + newVisitorStats.getPvPerSv() + "," +
            "\"old\":" + oldVisitorStats.getPvPerSv()
            + "}]}}";
        return json;

    }

    @RequestMapping("/province")
    public String getProvinceStats(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = now();
        }
        List<ProvinceStats> provinceStatsList = provinceStatsService.getProvinceStats(date);

        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceStatsList.size(); i++) {
            ProvinceStats provinceStats = provinceStatsList.get(i);
            jsonB.append(" {\"name\": \""+provinceStats.getProvince_name()+"\",\"value\": "+provinceStats.getOrder_amount()+"}");
            if(i < provinceStatsList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("],\"valueName\": \"交易额\"}}");
        return jsonB.toString();
    }

    @RequestMapping("/spu")
    public String getProductStatsBySpu(@RequestParam(value = "date", defaultValue = "0") Integer date) {

        if (date == 0) {
            date = now();
        }

        List<ProductStats> productStatsList = productStatsService.getProductStatsBySpu(date);
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": { \"columns\": [{\"name\": \"商品名称\", \"id\": \"name\"},{\"name\": \"交易额\",\"id\": \"amount\"},{\"name\": \"订单数\",\"id\": \"ct\"}],\"rows\": [");

        for (int i = 0; i < productStatsList.size(); i++) {
            ProductStats productStats = productStatsList.get(i);
            if(i >= 1){
                jsonB.append(",");
            }
            jsonB.append("{\"name\": \""+productStats.getSpu_name()+"\"," +
                "\"amount\": "+productStats.getOrder_amount()+"," +
                "\"ct\": "+productStats.getOrder_ct()+"}");

        }
        jsonB.append("]}}");

        return jsonB.toString();
    }
    @RequestMapping("/category3")
    public Object getProductStatsByCategory3(
        @RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "20") Integer limit) {

        if (date == 0) {
            date = now();
        }

        Map resMap = new HashMap();
        List dataList = new ArrayList();
        resMap.put("status",0);
        List<ProductStats> productStatsList = productStatsService.getProductStatsByCategory3(date, limit);
        for (ProductStats productStats : productStatsList) {
            Map dataMap = new HashMap();
            dataMap.put("name",productStats.getCategory3_name());
            dataMap.put("value",productStats.getOrder_amount());
            dataList.add(dataMap);
        }
        resMap.put("data",dataList);

        return resMap;
    }
    /*@RequestMapping("/category3")
    public String getProductStatsByCategory3(
        @RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "20") Integer limit) {

        if (date == 0) {
            date = now();
        }

        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": [");
        List<ProductStats> productStatsList = productStatsService.getProductStatsByCategory3(date, limit);
        for (int i = 0; i < productStatsList.size(); i++) {
            ProductStats productStats = productStatsList.get(i);
            jsonB.append("{\"name\": \""+productStats.getCategory3_name()+"\",\"value\": "+productStats.getOrder_amount()+"}");
            if(i < productStatsList.size() - 1){
                jsonB.append(",");
            }
        }

        jsonB.append("]}");
        return jsonB.toString();
    }*/
    @RequestMapping("/tm")
    public String getProductStatsByTm(
        @RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "20") Integer limit) {

        if (date == 0) {
            date = now();
        }
        List<ProductStats> productStatsList = productStatsService.getProductStatsByTm(date, limit);
        List tmList = new ArrayList();
        List amountList = new ArrayList();
        for (ProductStats productStats : productStatsList) {
            tmList.add(productStats.getTm_name());
            amountList.add(productStats.getOrder_amount());
        }

        String json = "{\"status\":0,\"data\":{" +
            "\"categories\":[\"" + StringUtils.join(tmList, "\",\"") + "\"]," +
            "\"series\":[{\"name\":\"商品品牌\",\"data\":["+StringUtils.join(amountList,",")+"]}]}}";

        return json;
    }

    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        //对参数date为null的情况进行处理
        if (date == 0) {
            date = now();
        }
        BigDecimal gmv = productStatsService.getGMV(date);
        String json = "{\"status\": 0,\"data\": " + gmv + "}";
        return json;
    }

    //获取今天日期
    private Integer now() {
        String yyyyMMdd = DateFormatUtils.format(new Date(), "yyyyMMdd");
        return Integer.valueOf(yyyyMMdd);
    }
}
